package com.heima.wemedia.service.impl;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.heima.client.article.IArticleClient;
import com.heima.model.common.dtos.ResponseResult;
import com.heima.model.common.dtos.WmStatisticsPageDto;
import com.heima.model.common.enums.AppHttpCodeEnum;
import com.heima.model.common.pojos.article.ApArticle;
import com.heima.model.common.pojos.wemedia.WmNewsStatistics;
import com.heima.model.common.vo.WmStatisticsVo;
import com.heima.utils.thread.WmThreadLocalUtil;
import com.heima.wemedia.mapper.WmStatisticsMapper;
import com.heima.wemedia.service.WmStatisticsService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;
import java.util.stream.Collectors;

@Service
public class WmStatisticsServiceImpl extends ServiceImpl<WmStatisticsMapper, WmStatisticsVo> implements WmStatisticsService {
    @Autowired
    private IArticleClient articleClient;

    /**
     * 数量统计
     *
     * @return
     */
    @Override
    public ResponseResult newDimension(String beginDate, String endDate) {

        Integer userId = WmThreadLocalUtil.get();
        if (userId == null) {
            return ResponseResult.errorResult(AppHttpCodeEnum.NEED_LOGIN);
        }
        //远程调用
        WmStatisticsVo wmStatisticsVo = new WmStatisticsVo();
        ResponseResult list = articleClient.getArticleList(beginDate, endDate, userId);
        if (list.getCode() == 200) {
            List<ApArticle> apArticleList = JSONObject.parseArray(JSON.toJSONString(list.getData()), ApArticle.class);

            if (!apArticleList.isEmpty()) {
                int likes = 0;
                int publicNum = 0;
                //处理用户喜欢数
                List<Integer> collect = apArticleList.stream()
                        .map(ApArticle::getLikes)
                        .collect(Collectors.toList());
                for (Integer i : collect) {
                    if (i != null) {
                        likes += i;
                    }

                }
                wmStatisticsVo.setLikesNum(likes);
                //数据收藏数
                List<Integer> collected = apArticleList.stream()
                        .map(ApArticle::getCollection)
                        .collect(Collectors.toList());
                for (Integer i : collected) {
                    if (i != null) {
                        publicNum += i;
                    }
                }
                wmStatisticsVo.setCollectNum(publicNum);
                //处理图文发布量
                wmStatisticsVo.setPublishNum(apArticleList.size());
            }
        }
        return ResponseResult.okResult(wmStatisticsVo);
    }

    /**
     * 分页查询
     *
     * @param
     * @return
     */
    @Override
    public ResponseResult newPage(String beginDate, String endDate, Integer page, Integer size) {
        Integer userId = WmThreadLocalUtil.get();
        if (page < 0) {
            page = 1;
        }
        if (size < 0 || size >= 50) {
            size = 10;
        }
        if (userId == null) {
            return ResponseResult.errorResult(AppHttpCodeEnum.NEED_LOGIN);
        }
        ResponseResult res = articleClient.getPage(beginDate, endDate, page, size, userId);
        if (res.getCode() == 200) {
            return res;
        }
        return ResponseResult.errorResult(AppHttpCodeEnum.SERVER_ERROR);
    }
}
